package com.ebelter.temperaturegun.ui.view.temview;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.ebelter.btcomlib.CommonLib;
import com.ebelter.btcomlib.utils.DipUtil;
import com.ebelter.temperaturegun.R;


public class TemHisChartViewTop extends View {

    private static final String TAG = "TemHisChartViewTop";
    public TemHisChartViewTop(Context context) {this(context,null);}

    public TemHisChartViewTop(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }







    public int dp1;

    private int topHeight;//顶部高度
    private int middleHeight;
    private int buttomHeight;



   


    int mWidth;
    int mHeight;
    Paint mPaint;

    private void init(Context context) {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//抗锯齿
        dp1 = DipUtil.dip2px(getContext(),1);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        initBaseData();
    }

    private void initBaseData() {

        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
        topHeight= (int) (mHeight*TemHisChartView.TopHeightWeight);
        buttomHeight= (int) (mHeight*TemHisChartView.ButtomHeightWeight);
        middleHeight= mHeight-topHeight-buttomHeight;
        out("---initBaseData--mWidth = "+mWidth+"--mHeight = "+mHeight+"---topHeight="+topHeight+"---middleHeight="+middleHeight+"---"+buttomHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        out("-----------onDraw--Start");


        //准备画正常体温的
        mPaint.setColor(Color.parseColor("#886D6C6E"));//设置字体颜色
        mPaint.setStyle(Paint.Style.FILL);
        float normalTop = (float) (mHeight-buttomHeight-(TemHisChartView.normalMaxTem - 32.5)/(41.5-32.5)*middleHeight);
        float normalButtom=(float) (mHeight-buttomHeight-(TemHisChartView.normalMinTem - 32.5)/(41.5-32.5)*middleHeight);
        canvas.drawRect(0,normalTop,mWidth,normalButtom,mPaint);

        drawTopLayout(canvas);

    }


    //画上层布局
    private void drawTopLayout(Canvas canvas) {


        //重新开一个图层
       // int layerId = canvas.saveLayer(0, 0, mWidth, mHeight, null, Canvas.ALL_SAVE_FLAG);

//        mPaint.setColor(Color.parseColor("#FFFFFF"));//白色
//        canvas.drawCircle(200,50,10,mPaint);//做个小实验 确定圆点是哪里开始算的


        mPaint.setColor(Color.parseColor("#90A3DA"));
        mPaint.setTextSize(DipUtil.sp2px(getContext(),12));
        mPaint.setStyle(Paint.Style.FILL);
        int tempItemHeght = middleHeight/TemHisChartView.Y_ITEMS_C.length;
        for (int i = 0;i < TemHisChartView.Y_ITEMS_C.length;i++){
            if (unitModel==0){//C
                canvas.drawText(TemHisChartView.Y_ITEMS_C[i],dp1*4,topHeight+(middleHeight-tempItemHeght*(i+1))+dp1*13,mPaint);
            }else {//F
                canvas.drawText(TemHisChartView.Y_ITEMS_F[i],dp1*4,topHeight+(middleHeight-tempItemHeght*(i+1))+dp1*13,mPaint);
            }

        }

        String zhchtw = CommonLib.getString(R.string.zhchtwen);
        int[] zhchtwWH=DipUtil.getTextWHeight(mPaint,zhchtw);
        int zhchtwX=mWidth-zhchtwWH[0]-dp1*3;
        int zhchtwY=zhchtwWH[1]+dp1*8;
        canvas.drawText(zhchtw,zhchtwX,zhchtwY,mPaint);

        //画右上角黄色矩形
        mPaint.setColor(Color.parseColor("#8E826A"));//黄色
        int hsjxLeft = zhchtwX-dp1*20;
        int hsjxTop = zhchtwY+dp1*-5;
        int hsjxRight = hsjxLeft+dp1*15;
        int hsjxButtom = hsjxTop+dp1*6;
        canvas.drawRect(hsjxLeft,hsjxTop,hsjxRight,hsjxButtom,mPaint);

        mPaint.setColor(Color.parseColor("#90A3DA"));
        String ychtw = CommonLib.getString(R.string.yichtiwen);
        int[] ychtwWH=DipUtil.getTextWHeight(mPaint,ychtw);
        int ychtwX=hsjxLeft-ychtwWH[0]-dp1*20;
        int ychtwY=zhchtwWH[1]+dp1*8;
        canvas.drawText(ychtw,ychtwX,ychtwY,mPaint);

        //画右上角的蓝色圆形
        mPaint.setColor(Color.parseColor("#76A3F3"));//蓝色
        mPaint.setStrokeWidth(dp1*2);
        mPaint.setStyle(Paint.Style.STROKE);
        int blueCirX = ychtwX-dp1*10;
        int blueCirY = zhchtwY+dp1*-5;
        canvas.drawCircle(blueCirX,blueCirY,dp1*5,mPaint);




    }

    private void out(String msg){
        Log.i(TAG,msg);
    }

    private int unitModel;
    //0代表°C 1代表°F
    public void setUnitModel(int unit) {
        this.unitModel=unit;
    }
}
